﻿Imports System.IO

Public Class frmPlaintiffEntry

    Dim _PhotoPath As String = ""
    Public _CaseID As Long

    Private Sub frmPlaintiffEntry_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        reset()
    End Sub

    Protected Sub reset()
        txtName.Clear()
        If cboGender.Items.Count > 0 Then
            cboGender.SelectedIndex = 0
        Else
            cboGender.SelectedIndex = -1
        End If
        dtpDOB.Value = #1/1/1980#
        txtAddress.Clear()
        txtTel.Clear()
        txtEmail.Clear()
        txtIDCard.Clear()
        picPhoto.Image = My.Resources.Photo
        _PhotoPath = ""
        txtName.Focus()
    End Sub

    Private Sub btnChoose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnChoose.Click
        If OpenFileDialog.ShowDialog() = Windows.Forms.DialogResult.OK Then
            _PhotoPath = OpenFileDialog.FileName
            picPhoto.ImageLocation = _PhotoPath
        End If
    End Sub

    Private Sub btnRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRemove.Click
        picPhoto.Image = My.Resources.Photo
    End Sub

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Close()
    End Sub

    Protected Function checknull() As Boolean
        Dim oc As New cControl
        If oc.checknull(txtName.Text) And oc.checknull(cboGender.Text) And oc.checknull(dtpDOB.Value) And oc.checknull(txtAddress.Text) And oc.checknull(txtTel.Text) And oc.checknull(txtEmail.Text) And oc.checknull(txtIDCard.Text) Then
            Return True
        Else
            Return False
        End If
    End Function

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Dim oPlaintiff As New cPlaintiff
        If checknull() Then
            If oPlaintiff.insert_Plaintiff(txtName.Text.Trim, cboGender.Text, dtpDOB.Value, txtAddress.Text, txtTel.Text.Trim, txtEmail.Text.Trim, txtIDCard.Text.Trim, getPhoto(), _CaseID, frmMain._User) Then
                MessageBox.Show("រក្សាទុកបានសម្រេច!", "រក្សាទុក", MessageBoxButtons.OK, MessageBoxIcon.Information)
                frmCaseEntry.getPlaintiff()
                reset()
            Else
                MessageBox.Show("រក្សាទុកមិនបានសម្រេចទេ", "រក្សាទុក", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If
        Else
            MessageBox.Show("សូមបញ្ចូលគ្រប់ពត៌មានទាំងអស់!", "រក្សាទុក", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If
    End Sub

    Protected Function getPhoto() As Byte()
        Dim pic As Byte() = Nothing
        If _PhotoPath <> "" Then
            pic = File.ReadAllBytes(_PhotoPath)
        End If
        Return pic
    End Function

    Private Sub btnClean_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClean.Click
        reset()
    End Sub
End Class